// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed

/**
 * @param {vscode.ExtensionContext} context
 */
function activate(context) {

	// Use the console to output diagnostic information (console.log) and errors (console.error)
	// This line of code will only be executed once when your extension is activated
	console.log('Congratulations, your extension "mycode" is now active!');

	// The command has been defined in the package.json file
	// Now provide the implementation of the command with  registerCommand
	// The commandId parameter must match the command field in package.json
	let disposable = vscode.commands.registerCommand('mycode.helloWorld', function () {
		// The code you place here will be executed every time your command is executed

		// Display a message box to the user
		vscode.window.showInformationMessage('Hello World from mycode!');
	});
	context.subscriptions.push(disposable);

	///////////////////////////////////my command//////////////////////////////////////
	//vscode.window.setStatusBarMessage("琳子今天早睡了吗")
	//vscode.window.setStatusBarMessage("|不赚钱的代码没什么意义,重要的事情是生活本身|")
	//vscode.window.setStatusBarMessage("|P770W|不赚钱的代码都是瞎扯,创业这件事是必须要做的|")
    // var care = "|idea|product|business mode|tech only tools|pygo pro|P770|"
    var care = " | Tech only tools, IPBM what really matters. P770W | "
    var care = " | Just for a living, Not any dream, Only life. P770W | "
	vscode.window.setStatusBarMessage(care)
// command: insertTime
	// var date = " [" + (new Date()).toISOString().split('.')[0] + "]"
	//var date = "# " + (new Date()).toISOString().split('T')[0] +' '+ (new Date()).toDateString().split(' ')[0] + "\n* TODO []" + "\n* TODO []\n\n"
	var date = "* TODO " + (new Date()).toISOString().split('T')[0] + ' []\n'
	var insertTime = insertStr('mycode.insertTime', date)
	context.subscriptions.push(insertTime);
	// command: changeTodo
	var changeTodo = replaceStr('mycode.changeTodo')
	context.subscriptions.push(changeTodo);
	///////////////////////////////////////////////////////////////////////////////////
}

// common method insertStr
function insertStr(commandName, insertString) {
	let insertResult = vscode.commands.registerCommand(commandName, function () {
		vscode.window.activeTextEditor.edit((editor) => {
			var position = new vscode.Position(
				0,0
				// vscode.window.activeTextEditor.selection.active.line,
				// vscode.window.activeTextEditor.selection.active.character
			)
			editor.insert(position, insertString)
		})
		// vscode.window.showInformationMessage(insertString);
	});
	return insertResult
}

// common method replaceStr
function replaceStr(commandName) {
	let insertResult = vscode.commands.registerCommand(commandName, function () {
		vscode.window.activeTextEditor.edit((editor) => {
			var currentLine = vscode.window.activeTextEditor.document.lineAt(
				vscode.window.activeTextEditor.selection.active.line)
			var l = "hello"
			if (currentLine.text.search(" TODO") > 0 ) {
				l = currentLine.text.replace(' TODO', 'DONE') + "*"
			} else {
				if (currentLine.text.search("DONE") > 0) {
					l = currentLine.text.replace('DONE', ' TODO')
					l = l.substring(0, l.length-1)
				}
			}
			editor.replace(currentLine.range, l)
		})
	});
	return insertResult
}

exports.activate = activate;

// this method is called when your extension is deactivated
function deactivate() {}

module.exports = {
	activate,
	deactivate
}
